var base_url = getApp().globalData.base_url;
var app = getApp();
Page({

  //登录后获取的用户信息
  data: {
    nickname: "游客",
    days: "",
    budgetAmount: "",
    isLoaded: false
  },

  onLoad: function () {
    this.refreshList()
  },

  onShow() {
    if (this.data.isLoaded) {
      // console.log("回到个人信息界面，重新渲染数据")
      this.setData({
        nickname: app.globalData.nickname,
        days: app.globalData.days,
        budgetAmount: app.globalData.budgetAmount
      })
    }
  },

  userPunch: function () {
    console.log("enter punch")
    let that = this
    wx.request({
      url: base_url + '/users/punches',
      method: 'POST',
      header: {
        "content-type": 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId
      },
      success(res) {
        if (res.data.resultCode == 200) {
          wx.request({
            url: base_url + '/users/punches/' + app.globalData.userId,
            method: 'GET',
            header: {
              "content-type": 'application/json'
            },
            success(res) {
              if (res.data.resultCode == 200) {
                app.globalData.days = app.globalData.days + 1
                that.onShow()
                wx.showToast({
                  title: '打卡成功',
                  icon: 'success',
                  duration: 3000
                })
              } else {
                wx.showToast({
                  title: '获取打卡天数失败',
                  icon: 'error',
                  duration: 3000
                })
              }
            }
          })

        } else {
          wx.showToast({
            title: '今天打过了',
            icon: 'error',
            duration: 3000//持续的时间
          })
          // TODO 页面提示获取个人信息失败
        }
      }
    })

  },


  logout() {
    let userId = app.globalData.userId

    wx.request({
      url: base_url + '/users/cookies/' + userId,
      method: 'DELETE',
      header: {
        "content-type": 'application/json'
      },

      success(res) {
        if (res.data.resultCode == 200) {
          // TODO 将app.js里面的全局变量都变成null
          wx.redirectTo({
            url: '../login/login',
          })
        } else {
          wx.showModal({
            title: "错误",
            content: res.data.message
          });
        }
      }
    })
  },

  refreshList() {
    let accountNumber = app.globalData.accountNumber
    var that = this;
    // 第一个请求，获取个人信息
    wx.request({
      url: base_url + '/users/' + accountNumber,
      method: 'GET',
      header: {
        "content-type": "application/json"
      },
      success(res) {
        if (res.data.resultCode == 200) {
          that.setData({
            days: res.data.data.days,
            nickname: res.data.data.nickname,
          })
          app.globalData.userId = res.data.data.userId;
          app.globalData.nickname = res.data.data.nickname;
          app.globalData.days = res.data.data.days;
          wx.request({
            url: base_url + '/users/budget/' + app.globalData.userId,
            method: 'GET',
            header: {
              "content-type": "application/json"
            },
            success(res) {
              if (res.data.resultCode == 200) {
                that.setData({
                  budgetAmount: res.data.data.budget,
                })
                app.globalData.budgetAmount = res.data.data.budget
                that.data.isLoaded = true
                console.log("全局变量设置结束")
              } else {
                wx.showToast({
                  title: '获取预算失败',
                  icon: 'false',
                  duration: 3000
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: '获取个人信息失败',
            icon: 'false',
            duration: 1000
          })
        }
      }
    })
  }

})